﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace XNAControls
{
    public class Radar : Panel
    {
        private List<RadarPoint> radarPoints;

        public Radar()
            : base()
        {
            radarPoints = new List<RadarPoint>();
        }

        /// <summary>
        /// Dodaje nowy punkt na radarze.
        /// </summary>
        /// <param name="position">Pozycja środka punktu.</param>
        /// <param name="texture">Tekstura punktu.</param>
        /// <param name="color">Maska koloru.</param>
        public void AddRadarPoint(Vector2 position, Texture2D texture, Color color, int id, bool active = true)
        {
            radarPoints.Add(
                new RadarPoint(
                    new Vector2(BackgroundTexture.Width / 2.0f + position.X - texture.Width / 2.0f,
                                BackgroundTexture.Height / 2.0f + position.Y - texture.Height / 2.0f), texture, color, id) { Active = active });
        }

        public void UpdateRadarPoint(int index, Vector2 newPosition, Color? newColor = null, bool? active = null)
        {
            if (index >= radarPoints.Count)
            {
                throw new ArgumentOutOfRangeException("Index out of range");
            }
            if (newPosition.Length() > Size.X / 2.0f || newPosition.Length() > Size.Y / 2.0f)
            {
                newPosition.Normalize();
                newPosition *= Size.X / 2.0f;
            }
            radarPoints[index].Position =
                new Vector2(BackgroundTexture.Width / 2.0f + newPosition.X - radarPoints[index].Texture.Width / 2.0f,
                            BackgroundTexture.Height / 2.0f + newPosition.Y - radarPoints[index].Texture.Height / 2.0f);
            if (newColor != null)
            {
                radarPoints[index].Color = (Color)newColor;
            }
            if (active != null)
            {
                radarPoints[index].Active = (bool)active;
            }
        }

        public void RemoveRadarPoint(int ID)
        {
            int index = -1;
            for (int i = 0; i < radarPoints.Count && index != -1; i++)
            {
                if (radarPoints[i].ID == ID)
                {
                    index = i;
                }
            }
            if (index >= 0)
            {
                radarPoints.RemoveAt(index);
            }
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            base.Draw(spriteBatch);
            spriteBatch.GraphicsDevice.BlendState = BlendState.Opaque;
            foreach (RadarPoint radarPoint in radarPoints)
            {
                if (radarPoint.Active)
                {
                    spriteBatch.Draw(radarPoint.Texture, new Vector2(radarPoint.Position.X + Position.X, radarPoint.Position.Y + Position.Y), radarPoint.Color);
                }
            }
            spriteBatch.GraphicsDevice.BlendState = BlendState.NonPremultiplied;
        }

        internal class RadarPoint
        {
            public Vector2 Position { get; set; }
            public Texture2D Texture { get; set; }
            public Color Color { get; set; }
            public int ID { get; set; }
            public bool Active { get; set; }

            public RadarPoint(Vector2 position, Texture2D texture, Color color, int id)
            {
                Position = position;
                Texture = texture;
                Color = color;
                ID = id;
                Active = true;
            }
        }
    }
}
